import logging

from src.globals.globals import GlobalManage
from src.handle.event_handler.handler_decorator import HandlerRegister
from src.log.logger import setup_logger
from src.model.event.events import DHTPingSendEvent
from src.rpc.KRPC import KRPC


@HandlerRegister.register
class PingSendHandler:
    """ping消息发送处理器"""
    
    def __init__(self, global_manage: GlobalManage):
        """初始化处理器
        
        Args:
            global_manage: 全局管理器实例
        """
        self.logger: logging.Logger = setup_logger("dht.ping_send")
        self.global_manage: GlobalManage = global_manage  
        
    @HandlerRegister.subscribe(DHTPingSendEvent)
    def handle_ping_send(self, event: DHTPingSendEvent) -> None:
        """处理ping消息发送事件
        
        Args:
            event: 引导节点启动事件
        """
        self.logger.debug("ping消息发送事件已处理")

        krpc:KRPC =  self.global_manage.get_krpc(event.unique_id)

        self.logger.debug(f"发送ping消息到{event.address}")
        
        krpc.send_ping_message(event.address)



    async def stop(self) -> None:
        """停止DHT引导节点"""
        self.logger.info("停止DHT引导节点")

            
            
        